<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\SupplierControl;
use App\Validate\Admin\Login;
use Illuminate\Http\Request;

/**
 * 后台登录
 */
class LoginController extends Controller
{

    /**
     * 定义模型
     */
    protected $model;

    /**
     * 定义验证
     */
    protected $validate;

    /**
     * 初始化控制器
     */
    public function __construct()
    {
        $this->validate = (new Login());
    }

    /**
     * 登录
     */
    public function login(Request $request)
    {
        $params = $request->all();
        // 规则验证
        if (!$this->validate->check($params, [], [], 'login')) {
            $this->error($this->validate->getError());
        }
        $supplier = (new SupplierControl())->where('username', $params['username'])->first();
        if (!$supplier) {
            $this->error('用户名不正确');
        }
        if ($supplier['status'] != 1) {
            $this->error('账号已被禁用');
        }
        if ($supplier['password'] != get_encrypt_password($params['password'], $supplier['salt'])) {
            $this->error('密码不正确');
        }
        $this->success(['username' => $supplier['username'], 'token' => uuid()]);
    }
}
